﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc.Html;
using TjZheng.Framework;
using System.Web.Mvc;

namespace TjZheng.EasyUI
{
    public static partial class EasyUIExtension
    {
        /// <summary>
        /// EasyUI文本框，验证数字
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">input的name属性</param>
        /// <param name="value">input的value属性</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <param name="precision">精度</param>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="validTypes">EasyUI提供的验证类型或扩展类型</param>
        /// <returns></returns>
        public static MvcHtmlString NumberBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes = null, int precision = 0, double minValue = 0, double maxValue = 0, params string[] validTypes)
        {
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            object data = null;
            List<string> dataOptionsList = new List<string>();
            if (objHtmlAttributes.TryGetValue("dataoptions", out data))
            {
                if (data.ToString().IsNoNull())
                {
                    dataOptionsList.Add(data.ToString());
                }
                objHtmlAttributes.Remove("dataoptions");
            }
            if (precision > 0)
            {
                dataOptionsList.Add(string.Format("precision:{0}", precision));
            }
            if (minValue < maxValue)
            {
                dataOptionsList.Add(string.Format("min:{0}", minValue));
                dataOptionsList.Add(string.Format("max:{0}", maxValue));
            }
            objHtmlAttributes["dataoptions"] = dataOptionsList.JoinComma();
            return htmlHelper.TextBox("easyui-numberbox", name, value, false, string.Empty, objHtmlAttributes, validTypes);

        }
    }
}
